import service from '@/utils/request.ts'
import { parseStrEmpty } from "@/utils/ruoyi.ts";
import {ApiResponse,ListApiResponse} from '@/store/dataType.ts'



export interface UserApiResponse {
  data:any
  postIds?: number[] //getUser
  roleIds: number[]  //getUser
  roles:any  //getUser
  posts:any  //getUser,getAuthRole
  roleGroup?:string //获取profile
  postGroup?:string //获取profile
  imgUrl?:string //头像上传
  user?:any //getAuthRole

}

export const API_USER = {
  // 查询用户列表
  listUser :(query:any):Promise<ListApiResponse> => {return service.get('/system/user/list',{params:query})},

    // 查询用户详细
  getUser: (userId: string | number): Promise<UserApiResponse> => {return service.get(`/system/user/${parseStrEmpty(userId)}`)},
    // 新增用户
  addUser: (data: Record<string, any>): Promise<ApiResponse> => {return service.post('/system/user', data)},

  // 修改用户
  updateUser: (data: Record<string, any>): Promise<ApiResponse> => {return service.put('/system/user', data)},

  // 删除用户
  delUser: (userId: string | number): Promise<void> => {return service.delete(`/system/user/${userId}`)},

  // 用户密码重置（管理员用）
  resetUserPwd: (userId: string | number, password: string): Promise<ApiResponse> => {
    return service.put('/system/user/resetPwd', { userId, password })
  },

  // 用户状态修改
  changeUserStatus: (userId: string | number, status: string): Promise<ApiResponse> => {return service.put('/system/user/changeStatus', { userId, status })},

  // 查询用户个人信息
  getUserProfile: (): Promise<UserApiResponse> => {return service.get('/system/user/profile')},

  // 修改用户个人信息
  updateUserProfile: (data: any): Promise<ApiResponse> => {
    return service.put('/system/user/profile', data)
  },

  // 用户密码修改（个人用）
  updateUserPwd: (oldPassword: string, newPassword: string): Promise<ApiResponse> => {
    return service.put('/system/user/profile/updatePwd', { oldPassword, newPassword })
  },

  // 用户头像上传
  uploadAvatar: (data: FormData): Promise<UserApiResponse> => {return service.post('/system/user/profile/avatar', data, {headers: { 'Content-Type': 'multipart/form-data' } })},

  // 查询授权角色
  getAuthRole: (userId: string | number): Promise<UserApiResponse> => {return service.get(`/system/user/authRole/${userId}`)},

  // 保存授权角色
  updateAuthRole: (data: { userId: string | number | undefined, roleIds: string}): Promise<ApiResponse> => {return service.put('/system/user/authRole',null, { params: data })},

  // 查询部门下拉树结构
  deptTreeSelect: (): Promise<ApiResponse> => {return service.get('/system/user/deptTree')}
}

